package com.example.studentstatusmanagement.controller;

import com.example.studentstatusmanagement.pojo.Classroom;
import com.example.studentstatusmanagement.pojo.Hygiene;
import com.example.studentstatusmanagement.pojo.Sturead;
import com.example.studentstatusmanagement.pojo.StureadExample;
import com.example.studentstatusmanagement.pojo.doo.ReqMajor;
import com.example.studentstatusmanagement.service.*;
import com.example.studentstatusmanagement.utils.ShiroUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * thymeleaf页面
 */
@Controller
public class PageController extends BaseController{

    @Resource
    private UserService userService;

    @Resource
    private ClassroomService classroomService;

    @Resource
    private PlotService plotService;

    @Resource
    private MajorService majorService;

    @Resource
    private DormService dormService;

    @Resource
    private MsgService msgService;

    // 学生信息查看
    @RequestMapping("/studentinfo")
    public String studentinfo(Integer id, Model model){
        model.addAttribute("studentinfo",userService.selectStudentInfoById(id));
        return "studentinfo";
    }

    // 学生信息修改
    @RequestMapping("/studentinfoupdate")
    public String studentInfoUpdate(Integer id, Model model){

        model.addAttribute("id",id);
        return "studentinfoupdate";
    }

    @RequestMapping("welcome")
    @ResponseBody
    public Map<String,Object> welcomepage(){
        Map<String,Object> welcome = new HashMap<>();
        welcome.put("user",getUser());
        welcome.put("number",userService.selectWelcomeNum());
        return welcome;
    }
    @RequestMapping("/logout")
    public String logout() {
        // 登出
        ShiroUtils.logout();

        return "redirect:/login.html";
    }
    @RequestMapping("majorupdate")
    public String majorupdate(ReqMajor reqMajor, Model model){
        model.addAttribute("major",reqMajor);
        return "majorupdate";
    }

    @RequestMapping("classroomupdate")
    public String classroomUpdate(Integer id, Model model){
        model.addAttribute("classroom",classroomService.selectByPrimaryKey(id));
        model.addAttribute("major",majorService.selectMajorsforClass());
        return "classupdate";
    }
    @RequestMapping("buildingupdate")
    public String buildingUpdate(ReqMajor reqMajor, Model model){
        model.addAttribute("building",reqMajor);
        return "buildingupdate";
    }

    @RequestMapping("plotupdate")
    public String plotUpdate(Integer id, Model model){
        model.addAttribute("plot",plotService.selectByPrimaryKey(id));
        model.addAttribute("building",plotService.selectBuildingsForPlot());
        return "plotupdate";
    }
    @RequestMapping("dormupdate")
    public String dormUpdate(Integer id, Model model){
        model.addAttribute("dorm",dormService.selectByPrimaryKey(id));
        model.addAttribute("plot",dormService.selectPlotsForDrom());
        return "dormupdate";
    }

    @RequestMapping("hygieneupdate")
    public String hygieneUpdate(Hygiene hygiene,Model model){
        model.addAttribute("hygiene",hygiene);
        return "hygieneupdate";
    }

    @RequestMapping("msginfo")
    public String msgInfo(Integer id,Model model){
        model.addAttribute("msg",msgService.selectByPrimaryKey(id));
        return "msginfo";
    }
    @RequestMapping("smsginfo")
    public String smsgInfo(Integer id,Model model){
        StureadExample stureadExample = new StureadExample();
        StureadExample.Criteria criteria = stureadExample.createCriteria();
        criteria.andMidEqualTo(id);
        criteria.andSidEqualTo(getUserId());
        if (msgService.selectByExampleStuRead(stureadExample).size() == 0){
            Sturead sturead = new Sturead();
            sturead.setMid(id);
            sturead.setSid(getUserId());
            msgService.insertSelectiveStuRead(sturead);
        }
        model.addAttribute("msg",msgService.selectByPrimaryKey(id));
        return "smsginfo";
    }

    @RequestMapping("updatepwd")
    public String updatePwd(Integer id,Model model){
        model.addAttribute("id",id);
        return "updatepassword";
    }

    @RequestMapping("myinfo")
    public String myinfo(Model model){
        model.addAttribute("studentinfo",userService.selectStudentInfoById(getUserId()));
        return "myinfo";
    }

}
